using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities;



namespace Services.Menu
{
    enum PopUpType
    {
        TwoStrings,
        TwoIcons,
        OneString,
        OneIcon
    }
    public class PopUpComponent : OrientableScreen
    {
        SpriteBatch spritebatch;
        InputManager input_manager;
        MyButton descriptionB, itemB;
        MyButton button0, button1;
        //Texture2D b0, b1;
        Texture2D bgText;
        Rectangle bgRect;
        //String s0,s1, description;
        Color bgColor;
        Action confirm;
        Action abort;
        //SpriteFont font;
        PopUpType type;
        #region Constructors
        int button_y = 320;
        int desc_y = 100;
        int item_y = 180;
        public PopUpComponent(Game game, Action confirm, Action abort, String description, String item, String s0, String s1, SpriteFont font, Texture2D box, Texture2D bg, Color bgColor)
            : base(game)
        {
            this.confirm = confirm;
            this.abort = abort;
            //this.description = description;
            //this.s0 = s0;
            //this.s1 = s1;
            //this.b0 = box;
            this.bgText = bg;
            this.bgColor = bgColor;
            type = PopUpType.TwoStrings;
            bgRect = new Rectangle(0, 0, 0, 0);
            button0 = new MyButton(new Rectangle(0, button_y, 100, 50), s0, Color.White, font, TextPos.Center, box, Color.White);
            button1 = new MyButton(new Rectangle(0, button_y, 100, 50), s1, Color.White, font, TextPos.Center, box, Color.White);
            descriptionB = new MyButton(new Rectangle(0, desc_y, 0, 100), description, Color.White, font);
            itemB = new MyButton(new Rectangle(0, item_y, 0, 100), item, Color.White, font);
        }
        public PopUpComponent(Game game, Action confirm, String description, String item, String s0, SpriteFont font, Texture2D box, Texture2D bg, Color bgColor)
            : base(game)
        {
            this.confirm = confirm;
            this.abort = null;
            //this.description = description;
            //this.s0 = s0;
            //this.b0 = box;
            this.bgColor = bgColor;
            this.bgText = bg;
            type = PopUpType.OneString;
            bgRect = new Rectangle(0, 0, 0, 0);
            button0 = new MyButton(new Rectangle(0, button_y, 100, 50), s0, Color.White, font, TextPos.Center, box, Color.White);
            descriptionB = new MyButton(new Rectangle(0, desc_y, 0, 100), description, Color.White, font);
            itemB = new MyButton(new Rectangle(0, item_y, 0, 100), item, Color.White, font);
        }
        public PopUpComponent(Game game, Action confirm, Action abort, String description, String item, SpriteFont font, Texture2D b0, Texture2D b1, Texture2D circle, Texture2D bg, Color bgColor)
            : base(game)
        {
            this.confirm = confirm;
            this.abort = abort;
            //this.description = description;
            //this.b0 = b0;
            //this.b1 = b1;
            this.bgColor = bgColor;
            this.bgText = bg;
            type = PopUpType.TwoIcons;
            bgRect = new Rectangle(0, 0, 0, 0);
            button0 = new MyButton(new Rectangle(0, button_y, 50, 50), b0, Color.White, circle, Color.White);
            button1 = new MyButton(new Rectangle(0, button_y, 50, 50), b1, Color.White, circle, Color.White);
            descriptionB = new MyButton(new Rectangle(0, desc_y, 0, 100), description, Color.White, font);
            itemB = new MyButton(new Rectangle(0, item_y, 0, 100), item, Color.White, font);
        }
        public PopUpComponent(Game game, Action confirm, String description, String item, SpriteFont font, Texture2D b0, Texture2D circle, Texture2D bg, Color bgColor)
            : base(game)
        {
            this.confirm = confirm;
            this.abort = null;
            //this.description = description;
            //this.b0 = b0;
            this.bgText = bg;
            this.bgColor = bgColor;
            type = PopUpType.OneIcon;
            bgRect = new Rectangle(0, 0, 0, 0);
            button0 = new MyButton(new Rectangle(0, button_y, 50, 50), b0, Color.White, circle, Color.White);
            descriptionB = new MyButton(new Rectangle(0, desc_y, 0, 100), description, Color.White, font);
            itemB = new MyButton(new Rectangle(0, item_y, 0, 100), item, Color.White, font);
        }
        #endregion
        public override void Initialize()
        {
            spritebatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            input_manager.Enabled = this.Enabled;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                Game.Components.Remove(this);
                Game.Components.Remove(input_manager);
                if (abort != null)
                    abort();
                else
                    confirm();
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(bgColor);

            
            spritebatch.Begin();
            spritebatch.Draw(bgText, bgRect, bgColor);
            descriptionB.Draw(spritebatch);
            itemB.Draw(spritebatch);
            button0.Draw(spritebatch);
            if(type== PopUpType.TwoIcons || type== PopUpType.TwoStrings)
                button1.Draw(spritebatch);
            //confirm.Draw(spritebatch);
            //cancel.Draw(spritebatch);
            spritebatch.End();

            base.Draw(gameTime);
        }
        void on_tap(Vector2 tap_pos)
        {
            if (press_wait < PRESS_INTERVAL) return;
            if (button0.tapped_on(tap_pos))
            {
                Game.Components.Remove(this);
                Game.Components.Remove(input_manager);
                confirm();
            }
            else if (button1 != null && button1.tapped_on(tap_pos))
            {
                Game.Components.Remove(this);
                Game.Components.Remove(input_manager);
                abort();
            }
        }
        
        public override void SetPortraitOrientation()
        {
            bgRect.Width = 480;
            bgRect.Height = 800;
            descriptionB.rectangle.Width = 480;
            itemB.rectangle.Width = 480;
            switch (type)
            {
                case PopUpType.OneIcon:
                    button0.rectangle.X = 215;
                    break;
                case PopUpType.OneString:
                    button0.rectangle.X = 190;
                    break;
                case PopUpType.TwoIcons:
                    button0.rectangle.X = 170;
                    button1.rectangle.X = 260;
                    break;
                case PopUpType.TwoStrings:
                    button0.rectangle.X = 120;
                    button1.rectangle.X = 260;
                    break;
            }
        }

        public override void SetLandScapeOrientation()
        {
            bgRect.Width = 800;
            bgRect.Height = 480;
            descriptionB.rectangle.Width = 800;
            itemB.rectangle.Width = 800;
            switch (type)
            {
                case PopUpType.OneIcon:
                    button0.rectangle.X = 375;
                    break;
                case PopUpType.OneString:
                    button0.rectangle.X = 350;
                    break;
                case PopUpType.TwoIcons:
                    button0.rectangle.X = 330;
                    button1.rectangle.X = 420;
                    break;
                case PopUpType.TwoStrings:
                    button0.rectangle.X = 280;
                    button1.rectangle.X = 420;
                    break;
            }
        }
    }
}
